package DGWeb.Servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import DGWeb.AppConstants;
import DGWeb.Model.model_CheckSession;
import DGWeb.Model.model_GlobalAnswer;


/**
 * Servlet implementation class checkSessionServlet
 */
@SuppressWarnings("deprecation")
@WebServlet("/checkSessionServlet")
public class checkSessionServlet extends HttpServlet implements SingleThreadModel {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public checkSessionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  		InputStreamReader isr = new InputStreamReader(request.getInputStream());
		BufferedReader br = new BufferedReader(isr);
		PrintWriter out = response.getWriter();
		HttpSession UserSession = request.getSession();
		
		// take the paremeters out
		Gson json = new Gson();
		model_CheckSession T = json.fromJson(br.readLine().toString(), model_CheckSession.class);
		String Type= T.getType();
		model_GlobalAnswer Answer = null ;
		String Username = (String) UserSession.getAttribute("Username");
		String NickName = (String) UserSession.getAttribute(AppConstants.NICKNAME);
		
		if ( Type.equals("mainWindow"))
		{

			if ( Username != null)
			{
				Answer = new model_GlobalAnswer(NickName,"","","Success");
				//out.print("Success,");
				//out.print(NickName);
				
			}
			else
			{
				Answer = new model_GlobalAnswer(NickName,"","index.html","Failed");
				//out.print("index.html");
			}
		}
		else if (Type.equals("loginWindow"))
		{
			
			if ( Username != null)
			{
				Answer = new model_GlobalAnswer("","","main.html","Success");
				//out.print("main.html");
				
			}
			else
			{
				Answer = new model_GlobalAnswer("","","","Failed");
				//out.print("");
			}
		}
		if ( Type.equals("TopicWindow"))
		{
			String Topic = (String) UserSession.getAttribute("Topic");
			if ( Topic != null)
			{
				Answer = new model_GlobalAnswer(NickName,Topic,"","Success");
				
			}
			else
			{
				Answer = new model_GlobalAnswer(NickName,"","main.html","Failed");
			}
		}
		
		if ( Type.equals("userProfile"))
		{
			
			String UserProfile = (String) UserSession.getAttribute("whichProfile");
			if ( UserProfile != null)
			{
				Answer = new model_GlobalAnswer("","","","Success");
				
			}
			else
			{
				Answer = new model_GlobalAnswer("","","main.html","Failed");
			}
		}
		
		String StringAnswer = json.toJson(Answer);
		out.print(StringAnswer);
		
		out.close();
		isr.close();
		br.close();
		
		
		
		
		
	}

}
